package com.hy.appium2;

import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class AppiumDemo4API {

    AndroidDriver driver;


    @BeforeMethod
    public void SetUp() throws MalformedURLException {
        //1.需要创建配置对象   desiredcapabilities
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //将需要启动的软件的一些配置添加进来
        //1.添加需要测试 的主机的路径
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "com.handsgo.jiakao.android");
        //自动化引擎
        desiredCapabilities.setCapability("automationName", "uiautomator2");
        //默认启动不初始化软件
        desiredCapabilities.setCapability("noReset", "true");
        desiredCapabilities.setCapability("appActivity", "com.handsgo.jiakao.android.splash.Login");
        //创建驱动
//        AndroidDriver<WebElement> Driver = new AndroidDriver<WebElement>(url, desiredCapabilities);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);


    }
    @Test
    public void test3() throws IOException {

        //判断是否安装
        boolean installed = driver.isAppInstalled("com.handsgo.jiakao.android");

        if (installed) {
            //重置
//        driver.resetApp();

            //获取网页源代码
////            String pageSource = driver.getPageSource();
//
//            System.out.println(pageSource);
//
//            //得到当前活动的activity
//
//            String currentActivity = driver.currentActivity();
//
//            System.out.println(currentActivity);

            System.out.println(installed);

            //获取设备的时间
            System.out.println("===============================================");

            String deviceTime = driver.getDeviceTime();
            System.out.println(deviceTime);

            //获取设备的DPI

            System.out.println(driver.getDisplayDensity());

            //获取自动化引擎

            System.out.println(driver.getAutomationName());

            //获取横竖屏的状态

            System.out.println(driver.getOrientation());

            //

            //截图

            File file = driver.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(file, new File("E:\\Appium\\test.png"));

            //要发送键值事件
            //需要实例化KeyEven对象    使用withkey创建键值
            //发送

//            KeyEvent keyEvent = new KeyEvent();
//
//            keyEvent.withKey(AndroidKey.HOME);
//
//            driver.pressKey(keyEvent);

        }
        else {
            System.out.println("NoApp");
        }





    }

    @AfterMethod
    public void TestDown() {


//        driver.quit();
    }

    public boolean byElement(By btId) {
        try {
            driver.findElement(btId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
